<?php
//测试支付宝交易
class test_alipay extends UnitTestCase{

	function setUp(){
		
		$this->_CI =& get_instance();
		$this->_CI->load->model( 'user_model' , '' , TRUE );
                $this->_CI->load->model( 'order_model' , '' , TRUE );
                $this->_CI->load->library( 'alipayescow' );
                Mock::generate('alipayescow');
		$this->_CI->load->database();

		$this->_CI->db->trans_begin();
		//插入一个测试用用户
		//已在数据库中添加了一个 id = 0
		$test_user_info = array(
			'user_id'=>0 ,
			'nick'=>'just_for_test' ,
			'email'=>'test@test.com' );

		if( !$this->_CI->user_model->is_exists( $test_user_info ) ){
			$this->_CI->user_model->add_new( 
				array(
					'user_id'=>0 ,
					'nick'=>'just_for_test' ,
					'email'=>'test@test.com' ) );
		}
		//插入测试商品以及服务信息
		$test_product_info = array(
			'product_id'=>'9999' ,
			'title'=>'test' ,
			'describe'=>'test' ,
			'price'=>'4.4'
		 );

		$test_product_class_info = array(
			'class_id'=>'9999' ,
			'order'=>'9999' ,
			'product_class_name'=>'tttt' ,
			'product_class_name_cn'=>'ttttttt'
		);

		$product_at_spec_class_info = array(
			'product_id'=>'9999' ,
			'product_class_id'=> '9999' ,
			'title'=>'testtest' ,
			'describe'=>'testtest' ,
			'price'=>'4.4'
		);

		$this->_CI->db->insert( 'product' , $test_product_info );
		$this->_CI->db->insert( 'product_class' , $test_product_class_info );
		$this->_CI->db->insert( 'product_at_spec_class' , $product_at_spec_class_info );

		//有效的新订单信息
		$this->new_order_valid = array(
			'user_id' => '0' ,
			'product_id' => '9999' ,
			'product_class_id' => '9999' ,
			'quantity' => '1' ,
			'price' => '4.4' ,
			'sum_price' => '323' ,
			'address' => 'test'
		 );

		//对于无效的订单信息 会触发一个异常
		$this->new_order_invalid = array(
			'user_id' => '0' ,
			'product_id' => '9999' ,
			'product_class_id' => '9999' ,
			'quantity' => '0' ,
			'price' => '213' ,
			'sum_price' => '323' ,
			'address' => 'ppppppppppppppp'
		);
	}
	function tearDown(){
		$this->_CI->db->trans_rollback();
	}

	function test_funtion_add_new(){
		//对于合法的订单信息
		$this->_CI->order_model->add_new( $this->new_order_valid );
		$new_order_id = $this->_CI->db->insert_id();
		$this->assertTrue( $this->_CI->order_model->is_exists( array( 'order_id'=>$new_order_id ) ) );
		//todo 对于非法的订单信息
		$this->expectException();
		$this->_CI->order_model->add_new( $this->new_order_invalid );
		$this->assertFalse( $this->_CI->order_model->is_exists( array( 'address'=>'ppppppppppppppp' ) ) );
	}

}

$test = new test_alipay();
$test->run( new HtmlReporter() );

